layui.define(['jquery', 'ax'], function (exports) {

    var $ = layui.$;
    var $ax = layui.ax;

    var $ZTree = function (id, url) {
        this.id = id;
        this.url = url;
        this.onClick = null;
        this.settings = null;
        this.ondblclick = null;
        this.filter = null;
    };

    $ZTree.prototype = {
        /**
         * 初始化ztree的设置
         */
        initSetting: function () {
            var settings = {
                async: {
                    enable: true,
                    url: "/sysDistrict/tree",
                    autoParam: ["id"],
                    dataFilter: filter
                },
                data: {
                    simpleData: {
                        enable: true,
                        idKey: 'id',
                        pIdKey: 'pid',
                        rootPId: 0
                    }
                },
                view: {
                    showIcon: true
                },
                callback: {
                    onClick: this.onClick,
                    onDblClick: this.ondblclick
                }
            };

            return settings;

        },
        init: function () {
            var zNodeSetting = this.initSetting();
            $.ajax({
                url: "/sysDistrict/tree",
                type: "post",
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    var zTreeObj = $.fn.zTree.init($("#sysDistrictTree"), zNodeSetting, data);
                    //让第一个父节点展开
                    var rootNode_0 = zTreeObj.getNodeByParam('id', 1, null);
                    zTreeObj.expandNode(rootNode_0, true, false, false, false);
                },
                error: function () {

                }
            });
        },


        /**
         * 绑定onclick事件
         */
        bindOnClick: function (func) {
            this.onClick = func;
        },

        /**
         * 绑定双击事件
         */
        bindOnDblClick: function (func) {
            this.ondblclick = func;
        },

        /**
         * 加载节点
         */
        loadNodes: function () {
            var zNodes = null;
            var ajax = new $ax(Feng.ctxPath + this.url, function (data) {
                zNodes = data;
            }, function (data) {
                Feng.error("加载ztree信息失败!");
            });
            ajax.start();
            return zNodes;
        },

        /**
         * 获取选中的值
         */
        getSelectedVal: function () {
            var zTree = $.fn.zTree.getZTreeObj(this.id);
            var nodes = zTree.getSelectedNodes();
            return nodes[0].name;
        },
        /**
         * 获取选中的值
         */
        getNodeById: function (treeId, id) {
            var node = $.fn.zTree.getZTreeObj(treeId).getNodeByParam("id", id);
            return node;
        }
    };

    function filter(treeId, parentNode, childNodes) {
        if (!childNodes)
            return null;
        childNodes = eval(childNodes);
        return childNodes;
    }

    exports('ztreeDistrict', $ZTree);

});